package com.baosight.risk.service.xtzy.impl;

import java.lang.reflect.Method;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baosight.risk.api.xtzy.IXtzyRoleShiftService;
import com.baosight.risk.base.mapper.BaseMapper;
import com.baosight.risk.base.service.BaseServiceImpl;
import com.baosight.risk.common.bean.BeanUtils;
import com.baosight.risk.common.utils.SpringUtils;
import com.baosight.risk.mapper.xtzy.XtzyRoleShiftMapper;
import com.baosight.risk.service.xtzy.entity.XtzyRoleShift;
@Service
public class XtzyRoleShiftServiceImpl extends BaseServiceImpl<XtzyRoleShift> implements IXtzyRoleShiftService {
    @Autowired
    private XtzyRoleShiftMapper roleShiftMapper;
    
	@Override
	public BaseMapper getMapper() {
		// TODO Auto-generated method stub
		return roleShiftMapper;
	}

	@Override
	@Transactional
	public Integer run(String compCode,String newUserCode,String oldUserCode,List<String> roleCodes) throws Exception{
		List<XtzyRoleShift> roleShifts = roleShiftMapper.selectByRoleCode(roleCodes);
		for(XtzyRoleShift roleShift : roleShifts){
			Object obj = SpringUtils.getBean(roleShift.getShiftClass());
			Class claz = obj.getClass();
			Method method = claz.getDeclaredMethod(roleShift.getShiftMethod(), String.class, String.class, String.class);
			method.invoke(obj, compCode,newUserCode,oldUserCode);
		}
		return 1;
	}

	@Override
	public List<XtzyRoleShift> listByRoleCode(String roleCode) {
		XtzyRoleShift param = new XtzyRoleShift();
		param.setRoleCode(roleCode);
		return roleShiftMapper.select(param);
	}

	@Override
	public int save(XtzyRoleShift shift) {
		if(StringUtils.isBlank(shift.getId())){
			shift = (XtzyRoleShift) BeanUtils.initBean(shift);
			return this.insert(shift);
		}else{
			return this.update(shift);
		}
	}

	@Override
	public int deleteByIds(List<String> ids) {
		for(String id: ids){
			roleShiftMapper.deleteByPrimaryKey(id);
		}
		return 1;
	}

}
